package com.youpai.platform.common.util;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

public class StringUtils {

	public static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM月dd日");
	public static final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("MM-dd");
	public static final DateFormat YEAR_MONTH_FORMAT = new SimpleDateFormat("yyyy-MM");
	public static final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");
	public static final DateFormat HMS_FORMAT = new SimpleDateFormat("HH:mm:ss");
	public static final DateFormat FULL_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final DateFormat MILLI_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	public static final DateFormat SIMPLE_FORMAT = new SimpleDateFormat("MM月dd日 HH:mm");
	public static final DateFormat SMS_FORMAT = new SimpleDateFormat("yyyyMMdd");
	public static final DateFormat YMD_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	public static final DateFormat FILE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	public static final DateFormat FULL_SECONDS_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
	public static final DateFormat FULL_SECONDS_SHORT_YEAR_FORMAT = new SimpleDateFormat("yyMMddHHmmss");
	public static final DateFormat FULL_MILL_SECONDS_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssms");
	public static final DateFormat YM_FORMAT = new SimpleDateFormat("yyyyMM");
	// 线程锁
	private static final String FORMAT_LOCK = "FORMAT";

	public static boolean isEmpty(String str) {
		return null == str || "".equals(str.trim());
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static Long parseLong(String value) {
		try {
			return Long.valueOf(value);
		} catch (Exception e) {
			return null;
		}
	}

	public static String timeToString(Object time, DateFormat format) {
		if (time == null) {
			return "";
		}
		try {
			synchronized(FORMAT_LOCK) {
				return format.format(time);
			}
		} catch (Exception e) {
			return "";
		}
	}

	public static Date parseDate(String value) {
		try {
			value = value.replace("/", "-");
			return Date.valueOf(value);
		} catch (Exception e) {
			return null;
		}
	}

	public static boolean isNumber(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

	public static String parseKeyword(String keyword) {
		if (org.apache.commons.lang3.StringUtils.isEmpty(keyword)) {
			return "";
		}
		return "%" + keyword + "%";
	}

	public static String idToString(Long id) {
		if (id == null) {
			return "";
		}
		return Long.toHexString(id);
	}

	public static Long getId(Long defaultId, Long generateId) {
		if(defaultId == null || generateId == null){
			return null;
		}
		String defaultIdS = String.valueOf(defaultId);
		String generateIdS = String.valueOf(generateId);
		String stringId = defaultIdS + generateIdS;
		return Long.valueOf(stringId.substring(0, 18));
	}
}
